package com.liang.publics.ad.ads;

import android.app.Activity;
import android.content.Context;
import android.os.CountDownTimer;
import android.util.Log;
import android.widget.FrameLayout;

import com.bytedance.sdk.openadsdk.mediation.manager.MediationFullScreenManager;
import com.bytedance.sdk.openadsdk.mediation.manager.MediationRewardManager;
import com.liang.publics.config.Const;


public     class AsService   {


    long millisInFuture = 12000;

    //---------------AD
    private FrameLayout mFeedContainer;
    //信息流
    private  String mFeedMediaId;
    //插屏
    private  String fullmMediaId;
    private  String fullmMediaId2;
    //激励
    private  String rewardmMediaId;

    public     int adCount=1;


    private static Activity activity;


    private MediationFeedService mediationFeedService;
    private MediationInterstitialFullService mediationInterstitialFullService;
    private MediationInterstitialFullService mediationInterstitialFullService2;
    private  MediationRewardService mediationRewardService;

    public void initAd(String type ,Context context,String adid,FrameLayout mFeedContainers){
        new Thread(new Runnable() {
            @Override
            public void run() {
//                Log.i(Const.TAG,"--------------------"+type);
                    if("feed".equals(type)) {

//                        if("0".equals(stype)){
//                            mFeedContainer = findViewById(R.id.fl_content);
//                        }else{
//                            mFeedContainer = findViewById(R.id.fl_content_s);
//                        }
                        if(mFeedContainers!=null) {
                            mFeedContainer = mFeedContainers;

                        }
                        if(mFeedContainer!=null) {
                            if(adid!=null){
                                mFeedMediaId = adid;
                            }

                            mediationFeedService = new MediationFeedService();
                            mediationFeedService.intiDate(context, mFeedContainer, mFeedMediaId);
                        }

                    }else if("full".equals(type)) {

                        // 插屏
                        if(adid!=null){
                            fullmMediaId = adid;
                        }
                        mediationInterstitialFullService = new   MediationInterstitialFullService();
                        mediationInterstitialFullService.intiDate(context, fullmMediaId);

                    }else if("full2".equals(type)) {


                        if(adid!=null){
                            fullmMediaId2 = adid;
                        }

                        mediationInterstitialFullService2 = new   MediationInterstitialFullService();
                        mediationInterstitialFullService2.intiDate(context, fullmMediaId2);

                    }else if("reward".equals(type)) {
                        if(adid!=null){
                            rewardmMediaId = adid;
                        }
                        //激励视频

                        mediationRewardService = new MediationRewardService();
                        mediationRewardService.intiDate(context, rewardmMediaId);

                    }
                }
        }).start();
    }




    public void showAd(String type,Activity  activitys){

        activity = activitys;

        if("feed".equals(type)) {
            CountDownTimer timerFeed = new CountDownTimer(millisInFuture, 100) {

                @Override
                public void onTick(long l) {


                    if(mediationFeedService!=null) {
                        Log.d(Const.TAG, "showAd---feed------------showFeedAdCount:" + mediationFeedService.showFeedAdCount);

                        if (mediationFeedService.mTTFeedAd != null && mediationFeedService.showFeedAdCount < adCount) {

                            mediationFeedService.showFeedAd(activity);
                        }
                    }
                }

                @Override
                public void onFinish() {

                }
            };
            timerFeed.start();
        }else if("full".equals(type)) {

            long duration = millisInFuture+2000;
            CountDownTimer timerFull = new CountDownTimer(duration, 100) {

                @Override
                public void onTick(long l) {
                    float num = (duration - l) / (float) duration * 100;
                    boolean ifShow = false;

                    if(mediationInterstitialFullService!=null) {
                        if (mediationInterstitialFullService.ifShowInterstitialFullAd == false) {
                            if (mediationInterstitialFullService.mTTFullScreenVideoAd != null && mediationInterstitialFullService.showInterstitialFullAdCount < adCount) {
//
//
//                            if (MediationFeedService.ifShowFeedAd == true && "0".equals(MediationFeedService.ifFeedShow)) {//成功
//                                ifShow = true;
//                            } else if (MediationFeedService.ifShowFeedAd == true && "2".equals(MediationFeedService.ifFeedShow)) {//失败
//                                ifShow = true;
//                            } else if (num > 95) {
//                                ifShow = true;
//                            }
//                        } else if (num > 95) {
//                            ifShow = true;
//                        }

                                if (mediationInterstitialFullService.ifOnFullScreenVideoCached == true) {
                                    MediationFullScreenManager manager = mediationInterstitialFullService.getManager();
                                    if (manager != null && manager.isReady()) {
                                        ifShow = true;
                                    } else if (num > 95) {
                                        ifShow = true;
                                    }
                                } else if (num > 95) {
                                    ifShow = true;
                                }
                            }

                        }
                        Log.d(Const.TAG, "showAd---full---------ifShowInterstitialFullAd:" + mediationInterstitialFullService.ifShowInterstitialFullAd + "----showInterstitialFullAdCount:" + mediationInterstitialFullService.showInterstitialFullAdCount + "-------l:" + num + "---------ifShow:" + ifShow);

                    }
                    if(ifShow){
                        mediationInterstitialFullService.showInterstitialFullAd(activity);
                    }
                }

                @Override
                public void onFinish() {

                }
            };
            timerFull.start();
        }else if("full2".equals(type)) {
            long duration = millisInFuture+2000;
            CountDownTimer timerFull = new CountDownTimer(duration, 100) {

                @Override
                public void onTick(long l) {
                    float num = (duration - l) / (float) duration * 100;
                    boolean ifShow = false;

                    if(mediationInterstitialFullService2!=null) {
                        if (mediationInterstitialFullService2.ifShowInterstitialFullAd == false) {
                            if (mediationInterstitialFullService2.mTTFullScreenVideoAd != null && mediationInterstitialFullService2.showInterstitialFullAdCount < adCount) {
//
//
//                            if (MediationFeedService.ifShowFeedAd == true && "0".equals(MediationFeedService.ifFeedShow)) {//成功
//                                ifShow = true;
//                            } else if (MediationFeedService.ifShowFeedAd == true && "2".equals(MediationFeedService.ifFeedShow)) {//失败
//                                ifShow = true;
//                            } else if (num > 95) {
//                                ifShow = true;
//                            }
//                        } else if (num > 95) {
//                            ifShow = true;
//                        }

                                if (mediationInterstitialFullService2.ifOnFullScreenVideoCached == true) {
                                    MediationFullScreenManager manager = mediationInterstitialFullService2.getManager();
                                    if (manager != null && manager.isReady()) {
                                        ifShow = true;
                                    } else if (num > 95) {
                                        ifShow = true;
                                    }
                                } else if (num > 95) {
                                    ifShow = true;
                                }
                            }

                        }
                        Log.d(Const.TAG, "showAd---full2---------ifShowInterstitialFullAd:" + mediationInterstitialFullService2.ifShowInterstitialFullAd + "----showInterstitialFullAdCount:" + mediationInterstitialFullService2.showInterstitialFullAdCount + "-------l:" + num + "---------ifShow:" + ifShow);

                    }
                    if(ifShow){
                        mediationInterstitialFullService2.showInterstitialFullAd(activity);
                    }
                }

                @Override
                public void onFinish() {

                }
            };
            timerFull.start();
        }else if("reward".equals(type)) {
            long duration = millisInFuture+2000;
            CountDownTimer rewardFull = new CountDownTimer(millisInFuture, 100) {

                @Override
                public void onTick(long l) {
                    float num = (duration - l) / (float) duration * 100;
                    boolean ifShow = false;

                    if(mediationRewardService!=null) {
                        if (mediationRewardService.ifShowRewardVideoAd == false) {
                            if (mediationRewardService.mTTRewardVideoAd != null && mediationRewardService.showRewardVideoAdCount < adCount) {

                                if (mediationRewardService.onRewardVideoCached == true) {
                                    MediationRewardManager manager = mediationRewardService.getManager();
                                    if (manager != null && manager.isReady()) {
                                        ifShow = true;
                                    } else if (num > 95) {
                                        ifShow = true;
                                    }
                                } else if (num > 95) {
                                    ifShow = true;
                                }


                            }
                        }
                        Log.d(Const.TAG, "showAd---reward---------ifShowRewardVideoAd:" + mediationRewardService.ifShowRewardVideoAd + "----showRewardVideoAdCount:" + mediationRewardService.showRewardVideoAdCount + "-------l:" + num + "---------ifShow:" + ifShow);
                    }
                    if(ifShow) {
                        mediationRewardService.showRewardVideoAd(activity);
                    }
                }

                @Override
                public void onFinish() {

                }
            };
            rewardFull.start();
        }

    }




}
